package com.qqtech.qquan.activity.controller;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.OrderException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseCostTypeDictEnum;
import com.qqtech.qquan.activity.enums.ActivityConst;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.model.ActivitySign;
import com.qqtech.qquan.activity.model.ActivitySignQuery;
import com.qqtech.qquan.activity.model.ActivitySignVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.activity.service.ActivitySignService;
import com.qqtech.qquan.activity.service.ActivitySysDictService;
import com.qqtech.qquan.order.enums.OrderConst;
import com.qqtech.qquan.order.model.OrderBase;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.order.service.OrderSysDictService;
import com.qqtech.qquan.user.enums.UserIdentityTypeDictEnum;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserIdentityQuery;
import com.qqtech.qquan.user.model.UserIdentityVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserIdentityService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/activity/sign")
public class ActivitySignController extends BaseWebController<ActivitySign, ActivitySignQuery> {
	@Resource
	private ActivitySignService activitySignService;
	
	@Resource
	private ActivityBaseService activityBaseService;
	
	@Resource
	private OrderBaseService orderBaseService;
	
	@Resource
	private ActivitySysDictService activitySysDictService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private OrderSysDictService orderSysDictService;
	
	@Resource
	private UserIdentityService userIdentityService;
	

	@Override
	protected BaseService<ActivitySign> getBaseService() {
		return activitySignService;
	}

	/**
	 * 分页
	 * 
	 * @param activitySignQuery
	 * @return
	 */
	@RequestMapping(value = "/page")
	@ResponseBody
	public Result page(@RequestBody ActivitySignQuery activitySignQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		
		Pageable pageKit = new PageKit(activitySignQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<ActivitySignVo> page = activitySignService.queryPageList(activitySignQuery, pageKit);
		UserBaseVo userBaseVo=null;
		String showPayStatus=null;
		boolean isActivityManager=false;
		ActivityBaseVo activityBaseVo=activityBaseService.getById(activitySignQuery.getActivityId());
		if(null!=curUserId&&curUserId.equals(activityBaseVo.getCreatorId())){//活动创建者查看报名列表
			isActivityManager=true;
		}
		UserIdentityVo userIdentityVo=null;
		UserIdentityQuery userIdentityQuery = new UserIdentityQuery();
		userIdentityQuery.setType(UserIdentityTypeDictEnum.PHONE.getCode());
		for (ActivitySignVo activitySignVo : page) {
			userBaseVo=userBaseService.getById(activitySignVo.getUserId());
			activitySignVo.setUserLogo(userBaseVo.getFullLogo());
			activitySignVo.setUserNick(userBaseVo.getNick());
			userIdentityQuery.setUserId(activitySignVo.getUserId());
			if(isActivityManager){
				userIdentityVo= userIdentityService.getOne(userIdentityQuery,  new PageOrder(Direction.DESC, "id"));
				activitySignVo.setUserPhone(userIdentityVo.getIdentity());//电话
			}
			if(StringUtil.isNotBlank(activitySignVo.getOrderNo())){//在线支付的
				OrderBase orderBase=orderBaseService.getByOrderNo(activitySignVo.getOrderNo());
				showPayStatus=orderSysDictService.dictValue(OrderConst.DICTYPE_ORDER_BASE_PAYSTATUS, String.valueOf(orderBase.getPayStatus()));
				activitySignVo.setShowPayStatus(showPayStatus);
				activitySignVo.setDeliveryStatus(orderBase.getDeliveryStatus());
			}
		}
		result.put("page", page);
		return result;
	}
	/**
	 * 分页
	 * 
	 * @param activitySignQuery
	 * @return
	 */
	@RequestMapping(value = "/littlePage")
	@ResponseBody
	public Result littlePage(@RequestBody ActivitySignQuery activitySignQuery) {
		Result result = new Result();
		Pageable pageKit = new PageKit(activitySignQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<ActivitySignVo> page = activitySignService.queryPageList(activitySignQuery, pageKit);
		UserBaseVo userBaseVo=null;
		for (ActivitySignVo activitySignVo : page) {
			userBaseVo=userBaseService.getById(activitySignVo.getUserId());
			activitySignVo.setUserLogo(userBaseVo.getFullLogo());
			activitySignVo.setUserNick(userBaseVo.getNick());
		}
		result.put("page", page);
		return result;
	}
	/**
	 * 分页
	 * 
	 * @param activitySignQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/manage/page")
	@ResponseBody
	public Result managePage(@RequestBody ActivitySignQuery activitySignQuery) {
		Result result = new Result();
		Integer curUserId = super.getUserId();
		activitySignQuery.setUserId(curUserId);
		Pageable pageKit = new PageKit(activitySignQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<ActivitySignVo> page = activitySignService.queryPageList(activitySignQuery, pageKit);
		ActivityBaseVo activityBaseVo=null;
		String typeName;
		for (ActivitySignVo activitySignVo : page) {
			activityBaseVo=activityBaseService.getById(activitySignVo.getActivityId());
			activityBaseVo.getShowStatus();
			activitySignVo.setActivityStatus(activityBaseVo.getStatus());
			activitySignVo.setActivityName(activityBaseVo.getName());
			activitySignVo.setActivityLogo(activityBaseVo.getLogo());
			activitySignVo.setActivityAddress(activityBaseVo.getAddress());
			if (activityBaseVo.getCostType() == ActivityBaseCostTypeDictEnum.ONLINE.getCode()) {// 在线交易
				activitySignVo.setShowCost("￥" + activityBaseVo.getCost());
			} else {
				typeName= activitySysDictService.dictValue(ActivityConst.DICTYPE_ACTIVITY_BASE_COSTTYPE,
						String.valueOf(activityBaseVo.getCostType()));
				activitySignVo.setShowCost(typeName);
			}
			activitySignVo.setShowStatus(activityBaseVo.getShowStatus());
		}
		result.put("page", page);
		return result;
	}
	/**
	 * 报名
	 * 
	 * @param activitySignVo
	 * @return
	 * @throws OrderException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/create")
	@ResponseBody
	public Result create(@RequestBody ActivitySign activitySign){
		Integer curUserId = super.getUserId();
		Result result=new Result();
		try {
			result=activitySignService.handleSign(curUserId, activitySign);
		} catch (OrderException e) {
			result.addErr(e.getMsg());
			result.setMsg(e.getMsg());
		}
		return result;
	}
	
	/**
	 *取消报名
	 * 
	 * @param activitySignVo
	 * @return
	 * @throws OrderException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/cancle")
	@ResponseBody
	public Result cancle(@RequestBody ActivitySignVo activitySignVo) throws OrderException {
		Result result = new Result();
		activitySignVo=activitySignService.getById(activitySignVo.getId());
		if(StringUtil.isBlank(activitySignVo.getOrderNo())){//没有支付的活动
			activitySignService.deleteById(activitySignVo.getId());
		}
		return result;
	}
	
	/**
	 *更改备注
	 * 
	 * @param activitySignVo
	 * @return
	 * @throws OrderException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/updateRemark")
	@ResponseBody
	public Result updateRemark(@RequestBody ActivitySignVo activitySignVo) throws OrderException {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		ActivitySignVo updateVo=activitySignService.getById(activitySignVo.getId());
		if(null==updateVo){
			ret.addErr("您报名的记录有误。");
			return ret;
		}
		if(updateVo.getUserId()!=curUserId.intValue()){
			ret.addErr("不是您报名的记录。");
			return ret;
		}
		activitySignService.updateById(activitySignVo);
		return ret;
	}
}
